package io.netty.example.http.websocketx.client.chatclient;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.example.http.websocketx.common.entity.NettyMessage;
import io.netty.example.http.websocketx.common.util.NettyMessageDecode;
import io.netty.example.http.websocketx.common.util.NettyMessageEncode;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.bytes.ByteArrayDecoder;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;


/**
 * @Description
 * @Author fuguangli businessfgl@163.com
 * @Create date:    2017/9/26
 */
public class ChatClientInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    public void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();

//        pipeline.addLast("decoder", new NettyMessageDecode());
//        pipeline.addLast("encoder", new NettyMessageEncode());
//        pipeline.addLast("handler", new ChatClientHandler());

//        pipeline.addLast("decoder",new ByteArrayDecoder());
//        pipeline.addLast("encoder", new ByteArrayEncoder());
        pipeline.addLast("decoder",new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,0,4,0,4,true));
//        pipeline.addLast("encoder", new LengthFieldPrepender(4,false));
        pipeline.addLast("encoder", new ByteArrayEncoder());
        pipeline.addLast("handler", new ChatClientHandler());
    }
}
